package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	pb "todolist/task-srv/proto/task"
)

// 定义TaskService
var service pb.TaskService

func Router(g *gin.Engine, taskService pb.TaskService) {
	// 初始化TaskService
	service = taskService
	v1 := g.Group("/task")
	{
		v1.GET("/search", Search)

	}
}

func Search(c *gin.Context) {
	req := new(pb.SearchRequest)
	if err := c.BindQuery(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  "bad param",
		})
		return
	}
	if resp, err := service.Search(c, req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  err.Error(),
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusOK,
			"data": resp,
		})
	}
}

func Finished(c *gin.Context) {
	req := new(pb.Task)
	if err := c.BindJSON(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": "500",
			"msg":  "bad param",
		})
		return
	}

	if resp, err := service.Finished(c, req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": "500",
			"msg":  err.Error(),
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": "200",
			"data": resp,
		})
	}
}
